<template>
  <ul class="list">
    <li
      class="item"
      v-for="item of citiesAlp"
      :key="item"
      :ref="item"
      @click="handleClick"
      @touchstart="handleTouchStart"
      @touchmove="handleTouchMove"
      @touchend="handleTouchEnd"
    >{{item}}</li>
  </ul>
</template>
<script>
export default {
  name: "alphabet",
  props: {
    cities: Object
  },
  data() {
    return {
      startY: 0,
      startStutes: null,
      timer: null
    };
  },
  updated() {
    this.startY = this.$refs["A"][0].offsetTop;
  },
  computed: {
    citiesAlp() {
      const letters = [];
      for (let i in this.cities) {
        letters.push(i);
      }
      return letters;
    }
  },
  methods: {
    handleClick(e) {
      this.$store.commit("citiesAlp/changeCitiesAlp", e.target.innerText);
    },
    handleTouchStart() {
      this.startStutes = true;
    },
    handleTouchMove(e) {
      if (this.startStutes) {
        if (this.timer) {
          clearTimeout(this.timer);
        }
        this.timer = setTimeout(() => {
          const touchY = e.touches[0].clientY - 80.5;
          const index = Math.floor((touchY - this.startY) / 19);
          if (index >= 0 && index <= this.citiesAlp.length) {
            this.$store.commit(
              "citiesAlp/changeCitiesAlp",
              this.citiesAlp[index]
            );
          }
        }, 16);
      }
    },
    handleTouchEnd() {
      this.startStutes = false;
    }
  }
};
</script>
<style lang="stylus" scoped>
@import '~@/assets/common/commonstyl/common.styl';

.list {
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: absolute;
  top: 158px;
  right: 0;
  bottom: 0;
  width: 40px;

  .item {
    line-height: 40px;
    text-align: center;
    color: $bgColor;
  }
}
</style>